package com.ets.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

public class VehicleEditPanel extends JPanel implements ActionListener
  {
   private static final long serialVersionUID = 1;	
		
   private static JLabel     editTitle,
                             stckLabel,
                             vinLabel,
                             yearLabel,
                             makeLabel; 
   private static JTextField stckField,
                             vinField,
                             yearField,
                             makeField;
   private static JButton    applyButton,
					         cancelButton,
					         clearButton,
					         exitButton;
   private static Border     loweredBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
	
   public VehicleEditPanel()
	 {
	  setLayout(new GridBagLayout());
      GridBagConstraints c = null;
	  this.setPreferredSize(new Dimension(800, 500)); 

      // Edit panel
      JPanel vehicleEditPanel = buildEditPanel();

      // Button panel
      JPanel vehicleButtonPanel = buildButtonPanel();
	   
      // Add sub-panels to main panel
      c = setConstraints(0, 0, 1, 1, GridBagConstraints.CENTER);
      this.add(vehicleEditPanel, c);
      c = setConstraints(0, 2, 1, 1, GridBagConstraints.WEST);
      this.add(vehicleButtonPanel, c);
	 }

   private JPanel buildEditPanel ()
     {
	  GridBagConstraints c      = null;
	  JPanel             ePanel = new JPanel();
      ePanel.setLayout(new GridBagLayout());
//      ePanel.setBorder(BorderFactory.createTitledBorder(boxBorder));
      ePanel.setBorder(loweredBorder);
//	  ePanel.setPreferredSize(new Dimension(800, 500)); 

      // Panel title
	  editTitle = new JLabel("V E H I C L E   D E T A I L  /  E D I T", 0);
	  c = setConstraints(0, 0, 2, 1, GridBagConstraints.CENTER);
	  ePanel.add(editTitle, c);
	     
      // Edit fields
      //  Stock number
	  stckLabel = new JLabel("Stock Number", 0);
	  c         = setConstraints(0, 1, 1, 1, GridBagConstraints.WEST);
	  ePanel.add(stckLabel, c);
      stckField = new JTextField(12);
      stckField.setEditable(true);
      stckField.setFocusable(true);
	  c         = setConstraints(1, 1, 1, 1, GridBagConstraints.WEST);
      ePanel.add(stckField, c);
      
      //  VIN
	  vinLabel = new JLabel("VIN", 0);
	  c         = setConstraints(0, 2, 1, 1, GridBagConstraints.WEST);
	  ePanel.add(vinLabel, c);
      vinField = new JTextField(18);
      vinField.setEditable(true);
      vinField.setFocusable(true);
	  c         = setConstraints(1, 2, 1, 1, GridBagConstraints.WEST);
      ePanel.add(vinField, c);
      
      // This should "listen" for the key strokes in the VIN field
      // and convert them to upper-case.
      
//	  vinField.addActionListener(this);
//      KeyListener.this.keyTyped(KeyEvent.KEY_TYPED);
      
      KeyListener keyListener = new KeyListener() {
//    	  String s = "";
          public void keyPressed(KeyEvent keyEvent) {
        	  
        	  vinField.setText(vinField.getText().toUpperCase());
//            vinField.setText(vinField.getText() + setToUpper(vinField.getText(), keyEvent));
//        	  str = str + (setToUpper(vinField.getText(), keyEvent));
          }
          public void keyReleased(KeyEvent keyEvent) {
//        	  s = vinField.getText();
        	  vinField.setText(vinField.getText().toUpperCase());
//        	  vinField.setText(vinField.getText() +setToUpper(vinField.getText(), keyEvent));
//        	  str = str + (setToUpper(vinField.getText(), keyEvent));
            }
          public void keyTyped(KeyEvent keyEvent) {
//        	  vinField.setText(vinField.getText() + setToUpper(vinField.getText(), keyEvent));
//        	  str = str + (setToUpper(vinField.getText(), keyEvent));
          }
      };

      vinField.addKeyListener(keyListener);
//      vinField.addKeyListener(keyListener);
      
      //  Year
	  yearLabel = new JLabel("Year", 0);
	  c         = setConstraints(0, 3, 1, 1, GridBagConstraints.WEST);
	  ePanel.add(yearLabel, c);
      yearField = new JTextField(4);
      yearField.setEditable(true);
      yearField.setFocusable(true);
	  c         = setConstraints(1, 3, 1, 1, GridBagConstraints.WEST);
      ePanel.add(yearField, c);
      //  Make
	  makeLabel = new JLabel("Make", 0);
	  c         = setConstraints(0, 4, 1, 1, GridBagConstraints.WEST);
	  ePanel.add(makeLabel, c);
      makeField = new JTextField(20);
      makeField.setEditable(true);
      makeField.setFocusable(true);
	  c         = setConstraints(1, 4, 1, 1, GridBagConstraints.WEST);
      ePanel.add(makeField, c);
     
	  return ePanel; 
     }

   private JPanel buildButtonPanel ()
     {
	  GridBagConstraints c      = null;
	  JPanel             bPanel = new JPanel();
//	  bPanel.setPreferredSize(new Dimension(800, 100)); 
	  bPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
      bPanel.setBorder(loweredBorder);
		
	  // Apply button
	  applyButton = new JButton("Apply changes");
	  bPanel.add(applyButton);
	  applyButton.setToolTipText("This is the apply button");
		
	  // Cancel button
	  cancelButton = new JButton("Cancel changes");
	  bPanel.add(cancelButton);
	  cancelButton.setToolTipText("This is the cancel button");
		
	  // Clear button
	  clearButton = new JButton("Clear all fields");
	  bPanel.add(clearButton);
	  clearButton.setToolTipText("This is the clear data button");
		
	  //Exit button
	  exitButton = new JButton("Exit");
	  exitButton.addActionListener(this);
	  bPanel.add(exitButton);
	  exitButton.setToolTipText("This is the exit button");
		
	  return bPanel; 
     }
   
//   private void setResizable(boolean b) {
//		// TODO Auto-generated method stub
//		
//	}

   private GridBagConstraints getConstraints(int gridx, int gridy,
    	    int gridwidth, int gridheight, int anchor)
    	    {
    	        GridBagConstraints c = new GridBagConstraints();
    	        c.insets = new Insets(5, 5, 5, 5);
    	        c.ipadx = 5;
    	        c.gridx = gridx;
    	        c.gridy = gridy;
    	        c.gridwidth = gridwidth;
    	        c.gridheight = gridheight;
    	        c.anchor = anchor;
    	        return c;
    	    }

   private GridBagConstraints setConstraints (int gx, int gy, int gw, int gh, int anc)
	 {
	  GridBagConstraints c = new GridBagConstraints();
	  c.insets             = new Insets(5, 5, 5, 5);
	  c.gridx              = gx;
      c.gridy              = gy;
	  c.gridwidth          = gw;
	  c.gridheight         = gh;
	  c.anchor             = anc;
	  return c;
	 }
	  
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		Object source = e.getSource();
		if (source == exitButton)
			System.exit(0);
//		else if (source == vinField)
//			if (e.)
//			vinField.setText(setToUpper(vinField.getText()));
		//	else if (source == calculateButton)
		//	{
		//  }

	}  
	
	private String setToUpper(String s, KeyEvent ke)
	{
		int keyCode = ke.getKeyCode();
		String str = KeyEvent.getKeyText(keyCode);
		System.out.println("s = " + s + " and str = " + str);
		s = str.toUpperCase();
		return s;
	}
	
  }
